﻿var sytdapp = {};
(function (p) {
    ///API服务地址
    var _APIHOST = "http://115.238.132.44:81/yczjapi/";
    p.APIHOST = _APIHOST;

    p.platform = {
        android: true,
        ios: false,
        isValid: function () {///是否有效
            return p.platform.android || p.platform.ios;
        }
    };
    p.Native_Util = {};
})(sytdapp);

sytdapp.Em_ErrMSG = {
    NoNetwork: "网络不通/服务停止",
    HttpErrorCallback: function (data, status) {
        switch (status) {
            case 404:
                break;
        }
        //可以根据状态扩展
        if (data !== null && typeof data === 'object') {
            alert(data.Message);
        } else {
            alert(this.NoNetwork);
        }
    }
};
sytdapp.LoggedUser = {//当前登录者
    key: "localStorage_LoggedUser",
    get: function () {//获取
        return window.localStorage.getItem(this.key);
    },
    set: function (data) {//设置
        window.localStorage.setItem(this.key, data);
    },
    clear: function () {//清除
        window.localStorage.removeItem(this.key);
    },
    islogged: function () {//是否登录
        var _val = this.get();
        return !(_val === null || _val === undefined);
    }
};

sytdapp.Native_Util.getURLAttribute = function (attrName) {//获取原生传递过来的参数
    var url = "";
    if (sytdapp.platform.android) {
        var url = window.location.search;
    } else if (sytdapp.platform.ios) {
        url = window.location.hash;
    }
    var re = eval("/" + attrName + "=([^&]*)/");
    if (re.test(url)) {
        return RegExp.$1;
    }
    else {
        return null;
    }
};

sytdapp.HTTPGET = function (typename, $http, url, successCallback, options) {
    var _options = {
        config: null,
        errorCallback: function (data, status, headers, config) {
            sytdapp.Em_ErrMSG.HttpErrorCallback(data, status);
        }
    };
    options = $.extend(_options, options);
    var _host = sytdapp.APIHOST;
    url = _host + url;
    $http.get(url, _options.config)
        .success(function (data) {
            successCallback(data);
        })
        .error(function (data, status, headers, config) {
            options.errorCallback(data, status, headers, config);
        });
}

sytdapp.HTTPPOST = function (typename, $http, url, data, successCallback, options) {
    var _options = {
        config: null,
        errorCallback: function (data, status, headers, config) {
            sytdapp.Em_ErrMSG.HttpErrorCallback(data, status);
        }
    };
    options = $.extend(_options, options);
    var _host = sytdapp.APIHOST;
    url = _host + url;
    $http.post(url, data, _options.config)
        .success(function (data) {
            successCallback(data);
        })
        .error(function (data, status, headers, config) {
            options.errorCallback(data, status, headers, config);
        });
}


